package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GoodsBean;
import com.project.mapper.IGoodsMapper;
import com.project.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GoodsService implements IGoodsService {
    @Autowired
    private IGoodsMapper goodsMapper;
    @Override
    public IPage<GoodsBean> cutByItem(Integer pageNo, String name,
                                      Integer startNum, Integer endNum, boolean price, String type) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        if (name != null && !name.equals("")) {
            qw.like("g_name",name);
        }
        if (startNum!=null && startNum!=0){
            qw.ge("g_num",startNum);
        }
        if (endNum!=null && endNum!=0){
            qw.le("g_num",endNum);
        }
        if (price) {
            qw.eq("g_newPrice",price);
        }
        if (type!=null && type!=""){
            qw.eq("g_sort",type);
        }
        return goodsMapper.selectPage(new Page<>(pageNo,2),qw);
    }

    @Override
    public GoodsBean findById(String id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public void updatePrice(String id, Integer newPrice) {
        GoodsBean goodsBean = goodsMapper.selectById(id);
        goodsBean.setMsrp(newPrice);
        goodsMapper.updateById(goodsBean);

    }

    @Override
    public void update(String goodsId, Integer newBuyPrice, Integer num) {
        GoodsBean goodsBean = goodsMapper.selectById(goodsId);
        goodsBean.setNewPrice(newBuyPrice);
        goodsBean.setNum(goodsBean.getNum()+num);
        goodsMapper.updateById(goodsBean);
    }

    @Override
    public void updateNum(String goodsId, Integer num) {
        GoodsBean goodsBean = goodsMapper.selectById(goodsId);
        goodsBean.setNum(goodsBean.getNum()-num);
    }

    @Override
    public void add(GoodsBean goodsBean) {
        goodsMapper.insert(goodsBean);
    }
}
